package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.POJO.OldHand;
import com.itheima.service.IOldHandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zqystart
 * @create 2022-09-01-11:26
 */
@RestController
@RequestMapping("/oldhand")
public class OldHandController {
    @Autowired
    private IOldHandService oldHandService;

    @GetMapping("/save")
    public R save(String username,int money,String goodsInfo,String image,Integer tel){//保存发布的二手交易货物
        OldHand oldHand=new OldHand();
        oldHand.setUsername(username);
        oldHand.setMoney(money);
        oldHand.setImage(image);
        oldHand.setGoodsInfo(goodsInfo);
        oldHand.setTel(tel);
        boolean isSuccess = oldHandService.save(oldHand);
        Map<String,Object> map=new HashMap<>();
        map.put("message",isSuccess);
        return R.ok(map);
    }

    @GetMapping("/selectAll")
    public R selectAll(){//查询所有的二手货物
        List<OldHand> list = oldHandService.list();
//        Page<OldHand> oldHandPage=new Page<>(1,4);
//        Page<OldHand> page = oldHandService.page(oldHandPage, new QueryWrapper<OldHand>().orderByDesc("id"));
//        List<OldHand> oldHandList = page.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("message",list);
        return R.ok(map);
    }

    @GetMapping("/select")
    public R select(Integer id){//通过id查询我发布的二手货物
        OldHand oldHand = oldHandService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("message",oldHand);
        return R.ok(map);
    }

    @GetMapping("/selectByUsername")
    public R selectByUsername(String username){//通过用户名查询我发布的二手货物
        List<OldHand> oldHandList = oldHandService.list(new QueryWrapper<OldHand>().eq("username", username));
        Map<String,Object> map=new HashMap<>();
        map.put("message",oldHandList);
        return R.ok(map);
    }

    @GetMapping("delete")
    public R delete(Integer id){//通过id删除顺带货物订单
        boolean remove = oldHandService.removeById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("message",remove);
        return R.ok(map);
    }
}
